package per.yhh.xxxtsj.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import per.yhh.xxxtsj.entity.Category;
import per.yhh.xxxtsj.entity.PageResult;
import per.yhh.xxxtsj.mapper.CategoryMapper;
import per.yhh.xxxtsj.service.CategoryService;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yhh
 * @since 2022-04-10
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    final
    CategoryMapper categoryMapper;

    public CategoryServiceImpl(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    @Override
    public List<Category> getCategories() {
        return categoryMapper.selectList(null);
    }

    @Override
    public PageResult<Category> getCategoriesByPage(Integer num, Integer size) {
        size = Math.min(size, 10);
        num = Math.max(num, 1);
        Page<Category> page = new Page<>(num, size);
        Page<Category> categoryPage = categoryMapper.selectPage(page, null);
        return new PageResult<>(categoryPage.getRecords(), categoryPage.getPages());
    }

    @Override
    public Boolean addCateGory(Category category) {
        return categoryMapper.insert(category) == 1;
    }


}
